﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using DbTest;

namespace ConsoleApplication1Tests
{
    [TestClass]
    public class TransacationScope怎么用
    {
        //TransactionScopeOption.Required 默认
        //TransactionScopeOption.RequiredNew 创建一个和原先无关的事务
        //TransactionScopeOption.Suppress 不使用事务
        [TestMethod]
        public void Test()
        {
            Db db = new Db();
            using (TransactionScope ts = new TransactionScope())
            {
                string str1 = Transaction.Current.TransactionInformation.LocalIdentifier;
                Console.WriteLine(str1);
                var obj = db.Insert("Address", new address { Info = "futianqu" }); ;
                var addressId = Convert.ToInt32(obj);
                Test2(db, addressId);

                ts.Complete();
            }
        }
        public void Test2(Db db, int addressId)
        {
            using (TransactionScope ts2 = new TransactionScope())
            {

                //string str2 = Transaction.Current.TransactionInformation.LocalIdentifier;
                //Console.WriteLine(str2);
                db.Insert("Person", new person { AddressId = addressId, Name = "liu" });
                ts2.Complete();
                //throw new Exception();
                //Assert.AreEqual(str1, str2);
            }
        }
    }
}
